package com.carmanagement

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

import org.springframework.security.access.annotation.Secured

@Secured(['ROLE_USER', 'ROLE_ADMIN'])
@Transactional(readOnly = true)
class VehicleController {

	static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE", paginateFullTank :"POST"]
	def vehicleService
	def chartsDatasService
	def statsService
	def springSecurityService

	def index(Integer max) {
		params.max = Math.min(max ?: 10, 100)
		def vehicles = []
		def vehiclesCount
		def user = springSecurityService.getCurrentUser()
		if(user.isAdmin()){
			vehicles = Vehicle.list(params)
			vehiclesCount = Vehicle.count()
		} else {
			vehicles = Vehicle.findAllByUser(user,params)
			vehiclesCount = Vehicle.countByUser(user)
		}
		respond vehicles, model:[vehicleInstanceCount: vehiclesCount]
	}

	def show(Vehicle vehicleInstance) {
		params.max = 5
		try{
			params.vehicleId = vehicleInstance.id

			def fullTankChartData = chartsDatasService.getDatasAndColumnsForFullTankPriceTrend(vehicleInstance)
			def fullTankPriceQuantChartData = chartsDatasService.getDatasAndColumnsForFullTankPriceQuantities(vehicleInstance)
			def fullTankDistanceChartData = chartsDatasService.getDatasAndColumnsForFullTankDistanceTrend( vehicleInstance )

			def fullTankStatsData = statsService.getStatsForFullTank(vehicleInstance)
			def maintenanceStatsData = statsService.getStatsForMaintenance( vehicleInstance )

			def maintenanceChartData = chartsDatasService.getDatasAndColumnsForMaintenancePriceTrend(vehicleInstance)

			respond vehicleInstance, model:[
				maintenanceChartData : maintenanceChartData,
				fullTankChartData : fullTankChartData,
				fullTankPriceQuantChartData : fullTankPriceQuantChartData,
				fullTankDistanceChartData : fullTankDistanceChartData,
				fullTankList : vehicleService.getFullTanks(vehicleInstance, params),
				fullTankCount : vehicleService.getFullTanksCount(vehicleInstance),
				fullTankStatsData : fullTankStatsData,
				maintenanceStatsData : maintenanceStatsData,
				maintanceList : vehicleService.getMaintenances(vehicleInstance, params),
				maintanceCount : vehicleService.getMaintenancesCount(vehicleInstance)]
		} catch(e){
			response.status = 404;
		}
	}

	def paginateFullTank(){
		Vehicle vehicleInstance = Vehicle.findById(params.vehicleId)
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		//		params.maxSteps = Math.min(params.maxSteps ? params.int('maxSteps') : 0, 10)
		def fullTankChartData = chartsDatasService.getDatasAndColumnsForFullTankPriceTrend(vehicleInstance)

		render (template: 'paginateFullTank', model: [
			fullTankChartData : fullTankChartData,
			fullTankList: vehicleService.getFullTanks(vehicleInstance, params),
			fullTankCount: vehicleService.getFullTanksCount(vehicleInstance)])
	}

	def paginateMaintenance(){
		Vehicle vehicleInstance = Vehicle.findById(params.vehicleId)
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		render (template: 'paginateMaintenance', model: [
			maintanceList: vehicleService.getMaintenances(vehicleInstance, params),
			maintanceCount: vehicleService.getMaintenancesCount(vehicleInstance)])
	}

	def create() {
		def vehicleInstance = new Vehicle(params)
		respond vehicleInstance
	}

	@Transactional
	def save(Vehicle vehicleInstance) {
		if (vehicleInstance == null) {
			notFound()
			return
		}
		vehicleInstance.clearErrors()
		def user = springSecurityService.getCurrentUser()
		vehicleInstance.user = user
		if (vehicleInstance.hasErrors()) {
			respond vehicleInstance.errors, view:'create'
			return
		}
		vehicleInstance.save flush:true

		request.withFormat {
			form {
				flash.message = message(code: 'default.created.message', args: [
					message(code: 'vehicleInstance.label', default: 'Vehicle'),
					vehicleInstance.id
				])
				redirect vehicleInstance
			}
			'*' { respond vehicleInstance, [status: CREATED] }
		}
	}

	def edit(Vehicle vehicleInstance) {
		respond vehicleInstance
	}

	@Transactional
	def update(Vehicle vehicleInstance) {
		if (vehicleInstance == null) {
			notFound()
			return
		}

		if (vehicleInstance.hasErrors()) {
			respond vehicleInstance.errors, view:'edit'
			return
		}

		vehicleInstance.save flush:true

		request.withFormat {
			form {
				flash.message = message(code: 'default.updated.message', args: [
					message(code: 'Vehicle.label', default: 'Vehicle'),
					vehicleInstance.id
				])
				redirect vehicleInstance
			}
			'*'{ respond vehicleInstance, [status: OK] }
		}
	}

	@Transactional
	def delete(Vehicle vehicleInstance) {

		if (vehicleInstance == null) {
			notFound()
			return
		}

		vehicleInstance.delete flush:true

		request.withFormat {
			form {
				flash.message = message(code: 'default.deleted.message', args: [
					message(code: 'Vehicle.label', default: 'Vehicle'),
					vehicleInstance.id
				])
				redirect action:"index", method:"GET"
			}
			'*'{ render status: NO_CONTENT }
		}
	}

	protected void notFound() {
		request.withFormat {
			form {
				flash.message = message(code: 'default.not.found.message', args: [
					message(code: 'vehicleInstance.label', default: 'Vehicle'),
					params.id
				])
				redirect action: "index", method: "GET"
			}
			'*'{ render status: NOT_FOUND }
		}
	}
}
